<?php
namespace Admin\Model;
use Think\Model;
class GmodelModel extends Model{

    protected $tableName = 'model';

    //通过模型获取规格
    public function param_all($model_id) {
        $model = M('model')->where( array('id'=>$model_id) )->find();
        $parameter = M('parameter')->where( array('model_id'=>$model_id) )->select();
        foreach ($parameter as $k=>$v) {
            $param[$v['p_name']] = M('parameter_val')->where( array('parameter_id'=>$v['id']) )->select();
        }
        $model['param'] = $param;
        return $model;
    }

    //获取属性和属性值（纯数据）
    public function all_attr_l($model_id) {
        $model = M('model')->where( array('id'=>$model_id) )->find();
        $attr = M('attr')->where( array('model_id'=>$model_id) )->select();
        foreach ($attr as $k=>$v) {
            $attrs[$v['a_name']] = M('attr_val')->where( array('attr_id'=>$v['id']) )->select();
        }
        $model['attrs'] = $attrs;
        return($model);
    }

    //获取属性和属性值（格式化的）
    public function all_attr($model_id) {
        //属性
        $attr = M('attr')->where( array('model_id'=>$model_id) )->select();
        $str = '<div style="margin-top: 30px;" id="param"><div class="formControls col-xs-8 col-sm-6"><table class="table table-border table-bordered table-bg table-sort"><thead>商品属性</thead><tbody>';
        foreach ($attr as $v) {
            //当时多选项的时候
            if($v['type'] == 2) {
                $attrVal = M('attrVal')->where( array('attr_id'=>$v['id']) )->select();
                $one = "<td><select name='attr[{$v['id']}]' class='select'> >";
                foreach ($attrVal as $x) {
                    $one .= "<option value='{$x['val']}'>{$x['val']}</option>";
                }
                $one .= "</select></td>";
                $str .= "<tr class='text-c'><td colspan='2'>{$v['a_name']}</td>$one</tr>";
            } else {
                //纯输入框的时候
                $str .= "<tr class='text-c'><td colspan='2'>{$v['a_name']}</td><td><input name='attr[{$v['id']}]' class='input-text radius size-S'></td></tr>";
            }
        }

        $str .= '</td></tr></tbody></table></div></div>';
        return $str;
    }


















}